define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        utils = require('js/utils/tools'),
        helper = require('js/apps/report/common');

    var SuperStrategyBlackView = Backbone.View.extend({
        events: {
            'change #game': 'changeGame',
            'change #direction': 'changeDirection',
            'click #addDebug': 'addDebug',
            'click a.onClickDeleteDebug': 'deleteDebug',
            'click a.onClickSaveDebug': 'saveDebug',
        },
        initialize: function (options) {
            this.options = options;
            this.s_list = [];
        },
        changeGame: function (e) {
            this.options.game = $('#game').val();
            this.load();
        },
        changeDirection: function (e) {
            this.options.direction = $('#direction').val();
            this.load();
        },
        addDebug: function (e) {
            var tr = $('<tr></tr>');
            tr.append('<td><input type="number" min="0" class="form-control" value="" required></td>');
            tr.append('<td><input type="number" placeholder="调控次数" min="1" class="form-control" value="" required></td>');
            tr.append('<td><input type="number" step=0.01 placeholder="调控概率" min="0.01" class="form-control" value="" required></td>');
            tr.append('<td><input type="number" placeholder="调控时间，单位秒" min="1" class="form-control" value="" required></td>');
            tr.append('<td><a class="glyphicons glyphicons-remove_2 onClickDeleteDebug" href="javascript:;"></a></td>');
            tr.append('<td><a class="glyphicons glyphicon-ok onClickSaveDebug" href="javascript:;"></a></td>');
            $('.debugTbody').append(tr);
        },
        deleteDebug: function (e) {
            var cells = $(e.currentTarget).closest('tr').find("td");
            var uid = parseInt($(cells[0]).find("input").val());
            if (uid) {
                var direction = $('#direction').val();
                var game = $('#game').val();
                $.post('/admin/super/strategy/'+direction+'/'+game+'/remove/'+uid+'/').done(function (resp) {
                    var data = resp.data;
                    notify.success('提示', '删除成功！');
                }).fail(function (data) {
                    notify.notifyResp(data);
                });
            }
            $(e.target).parent().parent().remove();
        },
        saveDebug: function (e) {
            var valid = true;
            var cells = $(e.currentTarget).closest('tr').find("td");
            var uid = parseInt($(cells[0]).find("input").val());
            var times = parseInt($(cells[1]).find("input").val());
            var ratio = parseFloat($(cells[2]).find("input").val());
            var ttl = parseInt($(cells[3]).find("input").val());
            if (!(uid) || !(times) || (!ratio) || !(ttl)) {
                notify.error('错误', '参数不能为空!');
                valid = false;
                return;
            }
            var confs = {
                "uid": uid,
                "times": times,
                "ratio": ratio,
                "ttl": ttl
            };
            console.log(confs);
            var direction = $('#direction').val();
            var game = $('#game').val();
            $.post('/admin/super/strategy/'+direction+'/'+game+'/set/', JSON.stringify(confs)).done(function (resp) {
                var data = resp.data;
                notify.success('提示', '保存成功！');
            }).fail(function (data) {
                notify.notifyResp(data);
            });
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        renderWithData: function () {
            this.$el.html(this.template({
                s_list: this.s_list,
                options: this.options,
            }));
            return this;
        },
        load: function () {
            var that = this;
            var direction = this.options.direction;
            var game = this.options.game;
            $.get('/admin/super/strategy/'+direction+'/'+game+'/get/').done(function (resp) {
                that.s_list = resp.data.list;
                that.renderWithData();
            }).fail(function (data) {
                notify.notifyResp(data);
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "op/SuperBlackList.html",
        render: function () {
            var view = new SuperStrategyBlackView({
                game: "bull",
                direction: "black"
            });
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();
            return this;
        }
    });

});
